class HomeController < ApplicationController
  
  def index
    @credit_record ||= CreditRecord.new
    @articles = Article.order(created_at: :desc).limit(18)

  end

  def admin
    respond_to do |format|
      format.html { redirect_to new_admin_session_path }
    end
  end

  def mobile
    
    respond_to do |format|
      format.json { render json: Models4mobile.routes_jsons, status: :ok }
    end
  end

  def ios_functions
    send_data( Models4mobile.gen_android_functions, :filename => "ios_functions.txt" )
  end

  def ios_models
    send_data( Models4mobile.gen_android_models, :filename => "ios_models.txt" )
  end

  def mobile_app
    logger.info '---->'+request.user_agent
    respond_to do |format|
      if request.user_agent.include?("QQ") || request.user_agent.include?("MicroMessenger") 
        format.html { render 'weixin_scan', layout: 'mobile' }
      else
        user_agent = UserAgent.parse(request.user_agent)
        p = user_agent.platform
        if p == 'Android'
          # @config = Config.where(name: 'android_apk').take
          # format.html { redirect_to @config.file.url, status: 301 }
          format.html { redirect_to 'http://xjgl.oss-cn-beijing.aliyuncs.com/static/graylist.apk', status: 301 }
        else p == 'iPhone'
          format.html { redirect_to 'itms://itunes.apple.com/cn/app/hui-ming-dan/id1076216229?l=zh&ls=1&mt=8', status: 301 }
          #format.html {render 'weixin_scan', layout: 'mobile'}#{ redirect_to 'itms://itunes.apple.com/cn/app/id529092160' }
          
        end
      end
    end
  end

  def credit_records_count
    td = Date.today
    tm = td+1
    tmt = tm.to_time
    tmt = tmt-1
    @today_count = CreditRecord.where(created_at: td.to_time..tmt).count
    _result = { today: @today_count, all: (CreditRecord.count+ 12580)}

    respond_to do |format|
      format.json { render json: _result.to_json, status: :ok }
    end
  end



  def about_us
    @config = Config.where(name: 'about_us').take
  end

  def service_term
    @config = Config.where(name: 'service_term').take
    respond_to do |format|
      format.html
      format.json { render json: @config.to_json, status: :ok }
    end
    
  end

  def contact_us
    @config = Config.where(name: 'contact_us').take
    
  end

  #举报
  def report
  	
  end

  #举报成功
  def reported
  	
  end

  def list_article
    @articles = Article.order(created_at: :desc).page params[:page]
  end


  def article
    @article = Article.find(params[:id])
  end

  def find_credit_records
    @credit_records =CreditRecord.with_approved_state.where(citizen_id: params[:citizen_id].upcase)
  end

  def show_credit_record
    @credit_record = CreditRecord.find(params[:id])
  end


  def smscode
    # @sms_verification = SmsVerification.new
    # @sms_verification.mobile = params[:mobile]
    # @sms_verification.save
    # r = @sms_verification.send_verification_code
    # respond_to do |format|
    #   if r['code'].to_i==2
    #     #format.html { redirect_to @sms_verification, notice: 'Sms verification was successfully created.' }
    #     #format.json { render :show, status: :created, location: @sms_verification }
    #     format.json { head :ok }
    #   else
    #     #format.html { render :new }
    #     #format.json { render json: @sms_verification.errors, status: :unprocessable_entity }
    #     format.json { head :bad_request }
        
    #   end
    # end

    @sms_verification = SmsVerification.new
    @sms_verification.mobile = params[:mobile]
    
    respond_to do |format|
      if @sms_verification.excced_sms_peroid_limit?
        format.json { render json: @sms_verification.excced_sms_peroid_limit_json, status: :bad_request }

      elsif @sms_verification.excced_sms_day_limit?
        format.json { render json: @sms_verification.excced_sms_day_limit_json, status: :bad_request }
      elsif @sms_verification.save
        if @sms_verification.send_verification_code
          format.json { head :ok }
        else
          format.json { render json: @sms_verification.ms_send_failed_json, status: :bad_request }
        end

      else
        #format.json { head :bad_request }
        format.json { render json: @sms_verification.errors, status: :unprocessable_entity }
      end
    end
  end

  def robots                                                                                                                                      
    robots = File.read(Rails.root + "public/robots.txt")
    render :text => robots, :layout => false, :content_type => "text/plain"
  end
  
end
